import asyncio
import json
import os
import sys
import subprocess
from threading import Thread

import requests

run_system = None


if sys.platform.startswith('linux'):
    # print('当前系统为 Linux')
    run_system = 'linux'
elif sys.platform.startswith('win'):
    # print('当前系统为 Windows')
    run_system = 'windows'
elif sys.platform.startswith('darwin'):
    # print('当前系统为 macOS')
    run_system = 'macOS'
else:
    # print('无法识别当前系统')
    run_system = "unknown"


def execute(shell_command_str=None, timeout=None, encoding="utf-8", check=True):
    assert shell_command_str is not None, "Please enter a shell command."

    result = subprocess.run(shell_command_str, shell=True, timeout=timeout, encoding=encoding, text=None, check=check,
                            capture_output=True)
    # returncode = 0 表示执行成功，其他整数表示执行异常
    return result.returncode, result.stdout, result.stderr


# ===================

def thread_fun(target, args):
    print("thread start")
    _thread = Thread(target=target, args=args)
    _thread.start()
    print("thread end")

# ===================


async def main():
    print('hello')
    await asyncio.sleep(1)
    print('world')



def async_fun(target, args):
    print("async start")
    asyncio.run(target)
    print("async end")

# ====================


def get_json_data(json_file):
    if not os.path.exists(json_file):
        assert False, "json file does not exist"
    with open(json_file, "r", encoding="utf-8") as f:
        json_data = json.load(f)
    return json_data

# ====================


# 自定义公共函数方法
# AI调用描述：调用代码模板call_third_party_api，去调用第三方接口，请求方法为%s，API的URL为%s，请求头%s，请求参数%s，并返回响应对象的解析数据%s。
def call_third_party_api(method, url, headers=None, params=None, data=None, json_data=None):
    """
    调用第三方 API

    :param method: HTTP 请求方法 (GET, POST, PUT, DELETE 等)
    :param url: API 的 URL
    :param headers: 请求头 (可选)
    :param params: GET请求参数: 查询参数 (可选)
    :param data: POST, PUT, DELETE 等请求参数: 表单数据 (可选)
    :param json_data: POST, PUT, DELETE 等请求参数: JSON 数据 (可选)
    :return: 响应对象
    """
    try:
        if headers is None:
            headers = {
                "Accept": "application/json",
                "Content-Type": "application/json",
            }

        response = requests.request(
            method=method,
            url=url,
            headers=headers,
            params=params,
            data=data,
            json=json_data
        )
        # 检查响应状态码
        if response.status_code == 200:
            return response.json()  # 返回 JSON 数据
        else:
            response.raise_for_status()  # 抛出异常，如果响应状态码不是 200
    except requests.RequestException as e:
        print(f"Request failed: {e}")
        return None


if __name__ == '__main__':
    print(run_system)
